{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "import matplotlib.pyplot as plt\n",
    "import matplotlib.animation as animation\n",
    "from IPython.display import HTML"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "import os.path\n",
    "sys.path.insert(0, os.path.expanduser(\"~/qaths/src/\"))\n",
    "try:\n",
    "    import qaths\n",
    "except ImportError:\n",
    "    print(\"Cannot import qaths!\")\n",
    "else:\n",
    "    print(\"qaths found!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1D wave equation solver\n",
    "\n",
    "This notebook illustrates how to use the `solve_1D_dirichlet_stationary` function to solve the wave equation for an arbitrary stationary initial condition and with Dirichlet boundary conditions $\\phi(x=0, t) = \\phi(x = 1, t) = 0$.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy \n",
    "min_time = 0.0\n",
    "max_time = 1.0\n",
    "time_slices = 100\n",
    "discretisation_points_number = 16\n",
    "trotter_order = 1\n",
    "epsilon = 1e-3\n",
    "fixed_r = None"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Execution and plot\n",
    "\n",
    "Now that everything is defined, we can solve the wave equation for different time slices and plot the evolution. In the figure generated by the cell below, the black line is for $t=0$ and the clearest line is for $t = \\text{max_time}$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "from qaths.snippets.animations.wave_solution_1d.compute_solutions import get_solutions\n",
    "\n",
    "_ = get_solutions(min_time, max_time, time_slices, discretisation_points_number, \n",
    "                  trotter_order, epsilon, verbose=True, fixed_r=fixed_r)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from qaths.snippets.animations.wave_solution_1d.generate_animation import get_js_animation\n",
    "js_anim = get_js_animation(min_time, \n",
    "                           max_time, \n",
    "                           time_slices, \n",
    "                           discretisation_points_number, \n",
    "                           trotter_order, \n",
    "                           epsilon, \n",
    "                           verbose=True, \n",
    "                           fixed_r=fixed_r)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "HTML(js_anim)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#from qaths.snippets.animations.wave_solution_1d.generate_animation import save_animation\n",
    "#save_animation(min_time, \n",
    "#               max_time, \n",
    "#               time_slices, \n",
    "#               discretisation_points_number, \n",
    "#               trotter_order, \n",
    "#               epsilon, \n",
    "#               video_extension=\"gif\",\n",
    "#               verbose=True,\n",
    "#               fixed_r=fixed_r)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "min_time = 0.0\n",
    "max_time = 2 * numpy.pi\n",
    "time_slices = 100\n",
    "discretisation_points_number = 32\n",
    "trotter_order = 1\n",
    "epsilon = 1e-3\n",
    "\n",
    "for min_time, max_time, time_slices, discretisation_points_number, trotter_order, epsilon in [\n",
    "    (0.0, 1, 100, 16, 1, 1e-1),\n",
    "    (0.0, 1, 100, 16, 2, 1e-1),\n",
    "    (0.0, 1, 100, 16, 3, 1e-1),\n",
    "    (0.0, 1, 100, 16, 1, 1e-3),\n",
    "    (0.0, 1, 100, 16, 2, 1e-3),\n",
    "    (0.0, 1, 100, 16, 3, 1e-3),\n",
    "    (0.0, 1, 100, 16, 1, 1e-5),\n",
    "    (0.0, 1, 100, 16, 2, 1e-5),\n",
    "    (0.0, 1, 100, 16, 3, 1e-5),\n",
    "    (0.0, 1, 100, 32, 1, 1e-3),\n",
    "    (0.0, 1, 100, 32, 2, 1e-3),\n",
    "    (0.0, 1, 100, 32, 3, 1e-3),\n",
    "    (0.0, 1, 100, 64, 1, 1e-3),\n",
    "    (0.0, 1, 100, 64, 2, 1e-3),\n",
    "    (0.0, 1, 100, 64, 3, 1e-3),\n",
    "    (0.0, 1, 100, 128, 1, 1e-3),\n",
    "    (0.0, 1, 100, 128, 2, 1e-3),\n",
    "    (0.0, 1, 100, 128, 3, 1e-3),\n",
    "]:\n",
    "    print(min_time, max_time, time_slices, discretisation_points_number, trotter_order, epsilon)\n",
    "    _ = get_solutions(min_time, max_time, time_slices, discretisation_points_number, trotter_order, epsilon, verbose=True)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.4.5"
  },
  "pycharm": {
   "stem_cell": {
    "cell_type": "raw",
    "metadata": {
     "collapsed": false
    },
    "source": []
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
